#!/usr/bin/env python
# -*- coding: utf-8 -*-
from utils import logutils
import subprocess

logger = logutils.getlogger(__file__)

def execute_command(cmd,funcname=None):
    if funcname:
        logger.info('****************> {} cmd [{}] start <****************'.format(funcname,cmd))
    else:
        logger.debug('****************> start <****************')
    logger.debug('start executing cmd:\n{}'.format(cmd))
    s = subprocess.Popen(str(cmd), stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
    stdoutinfo,stderrinfo = s.communicate()
    if funcname:
        logger.info('****************> {} cmd finish <****************'.format(funcname))
    else:
        logger.info('****************> finish <****************')
    return s.returncode,str(stdoutinfo,encoding="utf-8"),str(stderrinfo,encoding="utf-8")